package com.anhtuan.reminder.listelement;

import com.anhtuan.reminder.MainActivity;
import com.anhtuan.reminder.ReminderApplication;
import com.anhtuan.reminder.SelectionActivity;
import com.anhtuan.reminder.providers.TaskItem;

import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.BaseAdapter;

public abstract class BaseListElement {
	
	public OnClickListener getOnClickListener() {
		if (!activity.getClass().equals(MainActivity.class)) {
			return null;
		}
		
		return new OnClickListener() {

			@Override
			public void onClick(View view) {
				
				Intent intent = new Intent();
				intent.setClass(activity, SelectionActivity.class);
				intent.setData(SelectionActivity.EDIT_ACTION);
				intent.putExtra(MainActivity.TASK_ID_KEY, Integer.parseInt(getTag().toString()));
				activity.startActivityForResult(intent, getRequestCode());
				
			}
		};
	};
	
	public void onActivityResult(Intent data) {
		MainActivity mainActivity = (MainActivity) getActivity();
		ReminderApplication app = (ReminderApplication) mainActivity.getApplication();
		
		TaskItem taskItem = app.getTaskItem();
		
		if (taskItem != null) {
			mainActivity.startScheduleATask(taskItem); // Reset alarm
			mainActivity.updateTaskItem(getRequestCode(), taskItem);
		}
	}
	
	protected void onSaveInstanceState(Bundle bundle) {}
	protected boolean restoreState(Bundle savedState) {
	    return false;
	}
	protected void notifyDataChanged() {
	    adapter.notifyDataSetChanged();
	}
	
	private Drawable icon;
	private String text;
	private String description;
	private Object tag;
	private int requestCode;
	private BaseAdapter adapter;
	private Activity activity;
	
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
	public String getDescription() {
		return description;
	}
	public void setDescription(String description) {
		this.description = description;
	}
	public Drawable getIcon() {
		return icon;
	}
	public int getRequestCode() {
		return requestCode;
	}
	public void setRequestCode(int requestCode) {
		this.requestCode = requestCode;
	}
	public void setAdapter(BaseAdapter adapter) {
		this.adapter = adapter;
	}
	public Object getTag() {
		return tag;
	}
	public void setTag(Object tag) {
		this.tag = tag;
	}
	public Activity getActivity() {
		return activity;
	}
	public BaseListElement(Activity activity, Drawable icon, String text, String description,
			Object tag, int requestCode) {
		super();
		this.activity = activity;
		this.icon = icon;
		this.text = text;
		this.description = description;
		this.tag = tag;
		this.requestCode = requestCode;
	}	
	
}
